<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          default-expand-all
          row-key="id"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :cell-style="cellStyle"
          :cell-class-name="cellClassName"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" align="center" width="70" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column label="操作" align="center" fixed="right" width="240">
            <template slot-scope="scope">
              <div v-if="!scope.row.children" class="operate_template">
                <el-button
                  v-if="scope.row.status==0 && !scope.row.relateTaskId"
                  v-hasPermi="['product:task:edit']"
                  type="text"
                  @click.stop="edit(scope.row)"
                >编辑</el-button>
                <span v-else class="el-button--text button_grey">编辑</span>
                <el-button
                  v-if="scope.row.status ==0 && !scope.row.relateTaskId "
                  v-hasPermi="['product:task:delete']"
                  type="text"
                  @click.stop="deleteOne(scope.row)"
                >删除</el-button>
                <span v-else class="el-button--text button_grey">删除</span>
                <el-button
                  v-if="(scope.row.status ==3||scope.row.status ==2 || scope.row.status ==1) && !scope.row.planProductionDetailId"
                  v-hasPermi="['product:task:finish']"
                  type="text"
                  @click.stop="finish(scope.row)"
                >完成</el-button>
                <span v-else class="el-button--text button_grey">完成</span>
                <el-button
                  v-if="(scope.row.status ==3||scope.row.status ==2|| scope.row.status ==1) && !scope.row.planProductionDetailId"
                  v-hasPermi="['product:task:termination']"
                  type="text"
                  @click.stop="expiry(scope.row)"
                >终止</el-button>
                <span v-else class="el-button--text button_grey">终止</span>
              </div>

            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, newlist, listByPlan, remove, newremove, getInfo, finish, expiry, addOrUpdate, newgetInfo } from '@/api/ProductionManagement/taskSheet'
import { getInfo as getPlanInfo } from '@/api/planManagement/planProductionOrder'
import { getmateriaInfo } from '@/api/warehouseManagement/materialInfo'
import { getDictSelects } from '@/api/common/common'

import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
// import chooseFinishOrder from './chooseFinishOrder.vue'
import ShowDetail from '@/components/JLINK2/common/ShowDetailOld.vue' // 查看页面
import showPlanDetail from '@/views/planManagement/planProductionOrder/ShowDetailTitle.vue' // 查看页面
import ShowDeta from './ShowDeta' // 查看页面
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '生产任务单', // 页面唯一标识
      pageExportId: '生产任务单导出', // 页面导出标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'planProductionCode', label: '计划单号', minWidth: '200', isRegular: true },
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '任务单号', minWidth: '160' },
        { prop: 'taskTypeName', label: '任务类型', canOrder: false },
        { prop: 'materialCode', label: '物料编码', minWidth: '160' },
        { prop: 'materialName', label: '物料名称', canOrder: false },
        { prop: 'materialModel', label: '规格型号', canOrder: false },
        { prop: 'engineeringProcessName', label: '工序', canOrder: false },
        { prop: 'productBySelfName', label: '标记工序', canOrder: false },
        { prop: 'productNumber', label: '生产数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'reportTotalNumber', label: '报工数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'badnessTotalNumber', label: '不良数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'qualifiedTotalNumber', label: '合格数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'rateOfProgress', label: '进度', canOrder: false },
        { prop: 'productByName', label: '责任人', canOrder: false },
        { prop: 'finishDate', label: '完成日期', minWidth: '140', canOrder: false }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'planProductionCode', label: '计划单号', minWidth: '200' },
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '任务单号', minWidth: '200' },
        { prop: 'taskTypeName', label: '任务类型', canOrder: false },
        { prop: 'materialCode', label: '物料编码', minWidth: '200' },
        { prop: 'materialName', label: '物料名称', canOrder: false },
        { prop: 'materialModel', label: '规格型号', canOrder: false },
        { prop: 'engineeringProcessName', label: '工序', canOrder: false },
        { prop: 'productBySelfName', label: '标记工序', canOrder: false },
        { prop: 'productNumber', label: '生产数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'reportTotalNumber', label: '报工数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'badnessTotalNumber', label: '不良数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'qualifiedTotalNumber', label: '合格数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'rateOfProgress', label: '进度', canOrder: false },
        { prop: 'productByName', label: '责任人', canOrder: false },
        { prop: 'finishDate', label: '完成日期', minWidth: '140', canOrder: false }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'code', label: '任务单号', type: 'input' },
        { prop: 'materialName', label: '物料名称', type: 'input' },
        { prop: 'engineeringProcessName', label: '工序', type: 'input' },
        { prop: 'status', label: '状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'taskTypeCode', label: '任务类型', type: 'select', optionsName: 'taskStatusOptions' },
        { prop: 'planProductionCode', label: '计划单号', type: 'input' },
        { prop: 'materialCode', label: '物料编码', type: 'input' },
        { prop: 'materialModel', label: '规格型号', type: 'input' },
        // { prop: 'productNumber', label: '生产数量', type: 'input' },
        // { prop: 'completeNumber', label: '报工数量', type: 'input' },
        // { prop: 'badnessTotalNumber', label: '不良数量', type: 'input' },
        { prop: 'productByName', label: '责任人', type: 'input' },
        { prop: 'finishDate', label: '完成日期', type: 'time', isTime: true }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'product:task:add' },
        // { label: '导入', perm: '' },
        { label: '批量删除', perm: 'product:task:delete' },
        { label: '批量导出3', perm: 'product:task:export' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: '0', label: '待提交' },
        { value: '2', label: '待生产' },
        { value: '1', label: '待首检' },
        { value: '3', label: '生产中' },
        { value: '4', label: '已完成' },
        { value: '5', label: '已终止' }
      ],
      taskTpye: [],
      taskStatusOptions: [{ value: '0', label: '正常生产' },
        { value: '1', label: '生产返工' }],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '任务单号', key: 'code' },
          { type: 'form', name: '任务类型', key: 'taskTypeName' },
          { type: 'form', name: '计划单号', key: 'planProductionCode' },
          { type: 'form', name: '物料编码', key: 'materialCode' },
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '工艺要求', key: 'engineeringRequirementCodeVersion' },
          { type: 'form', name: 'BOM版本号', key: 'engineeringBomVersion' },
          { type: 'form', name: '工艺路线编码', key: 'engineeringPathCode' },
          { type: 'form', name: '工艺路线版本号', key: 'engineeringPathVersion' },
          { type: 'form', name: '工序', key: 'engineeringProcessName' },
          { type: 'form', name: '生产数量', key: 'productNumber' },
          { type: 'form', name: '责任人', key: 'productByName' },
          { type: 'form', name: '完成日期', key: 'finishDate' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          { type: 'form', name: '首件检验', key: 'qualityFirstItemInspectionName' }
        ]
      },
      // 查看计划单号详情的配置（根据配置自动生成查看页面）
      planShowkey: { // 查看详情的配置（根据配置自动生成查看页面）
        type: 'column_two',
        keys: [
          { type: 'form', name: '计划单号', key: 'code' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '销售订单', key: 'saleOrderCode' },
          { type: 'form', name: '计划类型', key: 'planTypeLabel' },
          { type: 'form', name: '计划人员', key: 'planPersonName' },
          { type: 'form', name: '计划日期', key: 'planTime' }, // length:'double' 表示占两列
          { type: 'Invoices', name: '生产任务单', key: 'enterWarehouseInvoices', length: 'double' },
          { type: 'form', name: '生产模式', key: 'modelName', length: 'double' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '物料明细', key: 'unmergeData', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '140' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '80' },
              { type: 'column', name: '工艺版本号', key: 'engineeringRequirementVersionCode', minWidth: '100' },
              { type: 'column', name: '客户交期', key: 'saleCustomerDeliveryTime', minWidth: '110' },
              { type: 'column', name: '订单数量', key: 'orderNumber', minWidth: '100' },
              { type: 'column', name: '库存数量', key: 'materialInventory', minWidth: '100' },
              { type: 'column', name: '计划数量', fixed: 'right', key: 'planNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '计划交期', key: 'planDealTime', minWidth: '110' },
              { type: 'column', name: '备注', key: 'remark', minWidth: '180' }
            ]
          },
          {
            type: 'table', name: '', key: 'productionTaskListVos', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '任务单号', key: 'code', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '任务类型', key: 'taskTypeName', minWidth: '100' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' },
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '工序', key: 'engineeringProcessName', minWidth: '100' },
              { type: 'column', name: '是否外协', key: 'productBySelfName', minWidth: '80' },
              { type: 'column', name: '计划数量', key: 'productNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '完成数量', key: 'finishNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '合格数量', key: 'qualifiedTotalNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '不合格数量', key: 'unqualifiedNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '进度', key: 'rateOfProgress', minWidth: '80' },
              { type: 'operate', name: '操作', key: 'schedule', minWidth: '60' }
            ]
          }
        ]
      },
      showKey1: { // 查看详情的配置（根据配置自动生成查看页面）
        type: 'column_two',
        keys: [
          { type: 'form', name: '计划单号', key: 'code' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '销售订单', key: 'saleOrderCode' },
          { type: 'form', name: '计划类型', key: 'planTypeLabel' },
          { type: 'form', name: '计划人员', key: 'planPersonName' },
          { type: 'form', name: '计划日期', key: 'planTime' }, // length:'double' 表示占两列
          { type: 'Invoices', name: '生产任务单', key: 'enterWarehouseInvoices', length: 'double' },
          { type: 'form', name: '生产模式', key: 'modelName', length: 'double' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '物料明细', key: 'unmergeData', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '140' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '80' },
              { type: 'column', name: 'BOM版本号', key: 'engineeringBomVersion', minWidth: '100' },
              { type: 'column', name: '工艺路线', key: 'engineeringPathCode', minWidth: '120' },
              { type: 'column', name: '工艺路线版本号', key: 'engineeringPathVersion', minWidth: '120' },
              { type: 'column', name: '客户交期', key: 'saleCustomerDeliveryTime', minWidth: '110' },
              { type: 'column', name: '订单数量', key: 'orderNumber', minWidth: '100' },
              { type: 'column', name: '库存数量', key: 'materialInventory', minWidth: '100' },
              { type: 'column', name: '计划数量', fixed: 'right', key: 'planNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '计划交期', key: 'planDealTime', minWidth: '110' },
              { type: 'column', name: '备注', key: 'remark', minWidth: '120' }
            ]
          },
          {
            type: 'table', name: '', key: 'productionTaskListVos', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '任务单号', key: 'code', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '任务类型', key: 'taskTypeName', minWidth: '100' }, // type: 'form' 表示是普通的表单展示

              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' },
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '工序', key: 'engineeringProcessName', minWidth: '100' },
              { type: 'column', name: '是否外协', key: 'productBySelfName', minWidth: '80' },
              { type: 'column', name: '计划数量', key: 'productNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '完成数量', key: 'finishNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '合格数量', key: 'qualifiedTotalNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '不合格数量', key: 'unqualifiedNumber', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '进度', key: 'rateOfProgress', minWidth: '80' },
              { type: 'operate', name: '操作', key: 'schedule', minWidth: '60' }

            ]
          }
        ]
      },
      materiashowKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '物料编码', key: 'materialCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '物料单位', key: 'materialUnitName' },
          { type: 'form', name: '物料分类', key: 'materialTypeName' }, // length:'double' 表示占两列
          { type: 'form', name: '物料属性', key: 'descriptionName' },
          { type: 'form', name: '重量', key: 'weight' },
          { type: 'form', name: '材质', key: 'texture' },
          { type: 'form', name: '品牌', key: 'brand' },
          { type: 'form', key: 'qualityGuaranteePeriod', name: '保质期(天)' },
          { type: 'form', key: 'minimumInventory', name: '最小库存量' },
          { type: 'form', name: '最大库存量', key: 'maximumInventory' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          { type: 'form', name: '状态', key: 'statusName' },
          { type: 'file', name: '物料配图', fileName: 'imgName', fileRealName: 'imgRealName', fileUrl: 'imgUrl' }
        ]
      }
    }
  },

  watch: {
    tableData() {
      this.$nextTick(() => {
        for (const elementNodeListOfElement of document.querySelectorAll('.SaleOrderCodeColumnClass')) {
          if (!elementNodeListOfElement.childNodes[0].childNodes[0].className || elementNodeListOfElement.childNodes[0].childNodes[0].className == 'commonTableColumn') {
            const paddingNode = document.createElement('div')
            paddingNode.className = 'SaleOrderCodeColumnClass_expand'
            paddingNode.innerHTML = ''
            elementNodeListOfElement.childNodes[0].insertBefore(paddingNode, elementNodeListOfElement.childNodes[0].childNodes[0])
          }
        }
      })
    }
  },
  created() {
    this.getDictOptions()
  },
  mounted() {
  },
  methods: {
    tableSelectable(row, index) {
      if (row.isNotselected) {
        return true
      } else {
        return false
      }
    },
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 1) {
        return 'SaleOrderCodeColumnClass'
      }
      if (!row.isNotselected) {
        return 'displayNoneClass'
      }
    },
    getDictOptions() {
      getDictSelects('taskTypeCode').then((res) => {
        this.taskTpye = res.data
      })
    },
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      } else if (column.property == 'planProductionCode' && row.planProductionCode && row.planProductionCode != '/') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      } else if (column.property == 'materialCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.ShowDeta(row)
          break
      }
      if (row.planProductionCode) {
        switch (column.property) {
          case 'planProductionCode':
            if (row.planProductionCode != '/') { this.showPlanDetailHandler(row) }
            break
        }
      }
      if (row.materialCode) {
        switch (column.property) {
          case 'materialCode':
            this.materiaShowDetail(row)
            break
        }
      }
    },
    // 生产任务详情
    ShowDeta(row) {
      newgetInfo(row.id).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDeta, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '570px'],
          title: '查看生产任务单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    // 计划单号详情
    showPlanDetailHandler(row) {
      console.log('row', row)
      let showKey = this.planShowkey
      getPlanInfo(row.planProductionId).then(res => {
        res.data.unmergeData = this.sumArr(res.data.unmergeData)
        // 计算合格数量
        res.data.productionTaskListVos.map((item) => {
          item.finishNumber = item.reportTotalNumber * 1 + item.badnessTotalNumber * 1
          item.unqualifiedNumber = item.disqualifiedTotalNumber * 1 + item.badnessTotalNumber * 1
          item.schedule = parseFloat((item.qualifiedTotalNumber * 100 / item.productNumber).toFixed(2)) + '%'
        })
        res.data.modelName = res.data.model == '1' ? '按BOM和工艺路线' : '按工艺要求'

        if (res.data.model == '1') {
          showKey = this.showKey1
        }
        this.$layer.iframe({// 弹框
          content: {
            content: showPlanDetail, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: showKey
            }// props
          },
          area: ['9999px', '9999px'],
          title: '查看生产计划单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },

    // 物料信息详情
    materiaShowDetail(row) {
      getmateriaInfo(row.materialId).then(res => {
        // 此处展示的页面有是否默认字段，后端给的是1，前端处理成 是
        if (res.data.data) {
          res.data.data.forEach(item => { item.isDefault == '1' ? item.isDefault = '是' : item.isDefault = '' })
        }
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetail, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.materiashowKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看物料信息', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    // 合并数据展示
    sumArr(arrList) {
      // this.fristFalg = false
      // // 转化为时间戳并且判断是否为第一次新增,是第一次则将未发数量合并带入计划数量
      // arrList.forEach((a) => {
      //   if (!a.planNumber) {
      //     this.fristFalg = true
      //   } else {
      //     this.fristFalg = false
      //   }
      //   // 新增时才转化时间戳计算取最小值
      //   if (!this.formData.id) {
      //     a.saleCustomerDeliveryTimeNumber = Date.parse(a.saleCustomerDeliveryTime.replace(/\-/g, '/'))
      //   }
      // })
      const nameArr = [...new Set(arrList.map(i => i.materialId))]// 名称集合
      const list = []
      nameArr.forEach(v => {
        const filterlist = arrList.filter(a => v == a.materialId)
        list.push(filterlist)
      })
      const outArr = []
      list.forEach((item, index) => {
        // if (!this.formData.id) {
        //   if (item.length > 1) {
        //     var min = Math.min.apply(Math, item.map(o => o.saleCustomerDeliveryTimeNumber))
        //     item[0].saleCustomerDeliveryTime = new Date(min).toLocaleDateString().slice().replace(/\//g, '-')
        //   }
        //   item[0].planDealTime = item[0].saleCustomerDeliveryTime
        // }
        // 新增第一次合并计算未发数量，否则计算计划数量
        // if (this.fristFalg) {
        //   // 合计订单数量
        //   const orderNumber = item.reduce((total, g) => {
        //     return total + Number(g.orderNumber)
        //   }, 0)
        //   const planNumber = item.reduce((total, g) => {
        //     return total + Number(g.willDeliverNumber)
        //   }, 0)
        //   outArr.push({
        //     ...item[0],
        //     orderNumber: orderNumber,
        //     planNumber: planNumber
        //   })
        // } else {
        // 合计订单数量
        const orderNumber = item.reduce((total, g) => {
          return total + Number(g.orderNumber)
        }, 0)
        const planNumber = item.reduce((total, g) => {
          return total + Number(g.planNumber)
        }, 0)
        outArr.push({
          ...item[0],
          orderNumber: orderNumber,
          planNumber: planNumber
        })
        // }
      })
      return outArr
    },
    getExportData(data) {
      // return newlist(data)
      return new Promise(function(resolve, reject) {
        const cacheList = []
        listByPlan(data).then(res => {
          console.log('res', res)
          res.data.forEach(item => {
            cacheList.push(item)
            if (item.children) {
              cacheList.push(...item.children)
            }
          })

          resolve({ data: cacheList })
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh2() {
      this.tableData = []
      setTimeout(() => {
        this.getList()
      }, 50)
    },
    getList() {
      listByPlan(this.pageData).then(res => {
        res.data.map((item) => {
          if (item.status != 0) {
            item.canDelete = 0
          }
          item.isNotselected = true
          if (!item.id) {
            // item.id = Date.now()
            item.id = uuidv4()
          }
        })
        this.tableData = res.data
        console.log('this.tableData', this.tableData)
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {}// props
        },
        area: ['1320px', '700px'],
        title: '新增生产任务单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      console.log('row', row)
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '700px'],
        title: '编辑生产任务单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    finish(row) {
      const _this = this
      this.$confirm('确定要完成吗', '完成确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        finish(row.id).then(res2 => {
          _this.$message({
            message: res2.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    expiry(row) {
      const _this = this
      this.$confirm('确定要终止吗', '终止确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        expiry(row.id).then(res2 => {
          _this.$message({
            message: res2.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        newremove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.selectionsAll = []
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return newremove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-tooltip{
  display: flex;
}
/deep/ .SaleOrderCodeColumnClass_expand{
  display: inline-block;
  width: 20px;
  line-height: 20px;
  height: 20px;
  text-align: center;
  margin-right: 3px;
}
/deep/.el-table .displayNoneClass .cell .el-checkbox__inner{
  display: none
}

</style>
